import { Message } from "element-ui";

// 生成api
/* config结构
  {
    name: 'Index',
    prefix: '/koa',
    apis: [{
        name: 'getMockData',
        path: '/string'
      },
      {
        name: 'getMockDataTest',
        path: '/test',
        type: 'post'
      }
    ]
  }
 */
import { request } from "./axios";

// 序列化 params
export function param(data) {
  let url = "";
  if (data) {
    for (const k of Object.keys(data)) {
      const value = data[k] !== undefined ? data[k] : "";
      url += `&${k}=${encodeURIComponent(value)}`;
    }
  }
  return url ? url.substring(1) : "";
}

// 参数转换为 formData
export function paramsToFormData(params) {
  const formData = new FormData();
  for (const k in params) {
    if (params[k] !== undefined) {
      formData.append(k, params[k]);
    }
  }
  return formData;
}

// 参数过滤 去掉undefined 和 空值
export const paramsFilter = params => {
  const query = {};
  for (const k in params) {
    if (params[k] !== undefined || params[k] !== null) {
      query[k] = params[k];
    }
  }
  return query;
};

let errCount = 0;
// 请求失败处理
export const handleError = err => {
  if (!err.message) return; // 重复请求
  if (err.message === "timeout of 10000ms exceeded")
    err.message = "请求超时，请检测网络或者联系管理员";
  if (err.message === "Network Error") err.message = "网络错误，请检查您的网络";
  if (errCount > 0) return;
  errCount += 1;
  Message({
    showClose: true,
    message: err.message,
    type: "error",
    onClose() {
      errCount -= 1;
    }
  });
};
// 操作成功处理
export const handleSuccess = message => {
  if (!message) return; // 有成功消息
  Message({
    showClose: true,
    message,
    type: "success"
  });
};

// export function generatorApis(config) {
//   const result = {}
//   for (const k of config.apis) {
//     result[k.name] = options => request(`${config.prefix}${k.path}`, options, k.type)
//   }
//   return result
// }

export class RestfulApi {
  constructor(url) {
    this.url = url;
  }

  _getPath(query) {
    let str = Object.values(query).join("/");
    str = str ? "/" + str : "";
    return `${this.url}${str}`;
  }

  // get({ query, ...args }) {
  //   return request(`${this._getPath(query)}`, { ...args }, 'get')
  // }

  // post({ query, ...args }) {
  //   return request(`${this._getPath(query)}`, { ...args }, 'post')
  // }

  // put({ query, ...args }) {
  //   return request(`${this._getPath(query)}`, { ...args }, 'post')
  // }

  // delete({ query, ...args }) {
  //   return request(`${this._getPath(query)}`, { ...args }, 'post')
  // }

  // 兼容
  add({ query, ...args }) {
    return request(`${this._getPath(query)}/add`, { ...args }, "post");
  }

  delete({ query, ...args }) {
    return request(`${this._getPath(query)}/delete`, { ...args }, "post");
  }

  detail({ query, ...args }) {
    return request(`${this._getPath(query)}/detail`, { ...args }, "get");
  }

  list({ query, ...args }) {
    return request(`${this._getPath(query)}/list`, { ...args }, "get");
  }

  update({ query, ...args }) {
    return request(`${this._getPath(query)}/update`, { ...args }, "post");
  }
}
